<?php
/**
 *
 * This file contains {@link Domain51_Service_Delicious}
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Service
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */

/**
 * Load required super class
 */
Solar::loadClass('Solar_Base');

/**
 *
 * The entry point for all calls to del.icio.us
 *
 * All connection to del.icio.us are made after being authenticated, so the
 * username and password for the del.icio.us account must be configured via your
 * Solar.config.php file.
 *
 * The API calls are taken directly from the 
 * {@link http://del.icio.us/help/api/ del.icio.us API page} with two 
 * exceptions, update has been moved to posts/update and bundles have been moved
 * to tags/bundles.  Reading their actual API call, you can see that update and
 * bundle actually are sub-calls within posts and tags respectively.
 *
 * Once configured, to instantiate a connect to the Delicious interface:
 * <code>
 *  $delicious = Solar::factory('Domain51_Service_Delicious');
 * </code>
 *
 * To determine the time of the most recent post:
 * <code>
 *  $delicious->posts->update()
 * </code>
 *
 * To retrieve an array of all tags:
 * <code>
 *  $delicious->tags->get()
 * </code>
 *
 * To retrieve a {@link Domain51_Service_Delicious_Posts_Container} of all posts
 * with a given tag:
 * <code>
 *  $delicious->posts->get('tagname');
 * </code>
 *
 *
 * This class implements a Strategy pattern for dispatching requests for 
 * multiple types of requests.  It delegates the actual API calls to known 
 * classes inside the Domain51/Service/Delicious/ directory.  See the 
 * {@link http://www.phppatterns.com/docs/design/strategy_pattern Strategy article on phppatterns.com}
 * for more information on the pattern.
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Service
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 * @todo package level: refactor out concrete class depdencies on
 *       {@link Domain51_Util_Service_Connection}
 *
 */
 
class Domain51_Service_Delicious extends Solar_Base
{
    /**
     * User-provided configuration values
     *
     * Keys are:
     * `username`
     * : (string) A del.icio.us username
     *
     * `password`
     * : (string) A del.icio.us password 
     *
     * @var     array
     * @access  protected
     */
    protected $_Domain51_Service_Delicious = array(
        'username' => '',
        'password' => ''
    );
    
    private $_connection = null;
    
    
    /**
     *
     * Handle instantiation
     *
     * @param array $config
     *
     */
    public function __construct($config = null) {
        parent::__construct($config);
        
        $connection_config = $this->_config;
        $connection_config['service_url'] = 'https://api.del.icio.us/v1/';
        
        $this->_connection = Solar::factory(
            'Domain51_Util_Service_Connection',
            $connection_config
        );
    }
    
    
    /**
     *
     * Serves as the Stragetgy object for determining what type of call this is
     * and returning the proper class.
     *
     * This will return one of the objects from the Domain51_Service_Delicious_*
     * objects depending on the method.
     *
     * @param string $method
     *
     * @param array $arguments
     *
     * @return object
     *
     */
    public function _get($method) {
        $method = strtolower($method);
        switch ($method) {
            case 'tags' :
            case 'posts' :
                $adapter = ucfirst($method);
                break;
            
            default:
                throw $this->_exception(
                    'ERR_UNKNOWN_METHOD',
                    array('method' => $method)
                );
        }
        
        $adapterClass = 'Domain51_Service_Delicious_' . $adapter;
        Solar::loadClass($adapterClass);
        return new $adapterClass($this->_connection); 
    }
}

?>